package com.lagou.edu.transfer.service.impl;

import com.lagou.edu.spring.annotations.Autowired;
import com.lagou.edu.spring.annotations.Service;
import com.lagou.edu.spring.mybatis.transaction.MyTransactional;
import com.lagou.edu.transfer.dao.AccountDao;
import com.lagou.edu.transfer.pojo.Account;
import com.lagou.edu.transfer.service.TransferService;

/**
 * @author 应癫
 */
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;


    @Override
    @MyTransactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
//        int c = 1 / 0;
        accountDao.updateAccountByCardNo(from);

    }

}
